home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / program / gcc / ixemul-4.lha / ixemul-41.4 / stdlib / qsort.c < prev    next >
C/C++ Source or Header  |  1995-05-17  |  7KB  |  245 lines

  1. /*-
  2.  * Copyright (c) 1980, 1983 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. /* Modified by PerOla Valfridsson 910215 */
  21.  
  22. #if defined(LIBC_SCCS) && !defined(lint)
  23. static char sccsid[] = "@(#)qsort.c    5.7 (Berkeley) 5/17/90";
  24. #endif /* LIBC_SCCS and not lint */
  25.  
  26. #define KERNEL
  27. #include "ixemul.h"
  28.  
  29. /* this function uses static variables to cut down stack-usage to a 
  30.  * minimum. Since I don't want to put those variables back on the stack,
  31.  * I'll lock entry/exit to qsort() with semaphores */
  32.  
  33. #include <exec/semaphores.h>
  34.  
  35. static struct SignalSemaphore ss;
  36. static int semaphore_initialized = 0;
  37.  
  38. #include <stdlib.h>
  39.  
  40. static  qst ();
  41.  
  42. /*
  43.  * qsort.c:
  44.  * Our own version of the system qsort routine which is faster by an average
  45.  * of 25%, with lows and highs of 10% and 50%.
  46.  * The THRESHold below is the insertion sort threshold, and has been adjusted
  47.  * for records of size 48 bytes.
  48.  * The MTHREShold is where we stop finding a better median.
  49.  */
  50.  
  51. #define        THRESH        4        /* threshold for insertion */
  52. #define        MTHRESH        6        /* threshold for median */
  53.  
  54. static  int        (*qcmp)();        /* the comparison routine */
  55. static  int        qsz;            /* size of each record */
  56. static  int        thresh;            /* THRESHold in chars */
  57. static  int        mthresh;        /* MTHRESHold in chars */
  58.  
  59. /*
  60.  * qsort:
  61.  * First, set up some global parameters for qst to share.  Then, quicksort
  62.  * with qst(), and then a cleanup insertion sort ourselves.  Sound simple?
  63.  * It's not...
  64.  */
  65.  
  66. void
  67. qsort(void *base, size_t n, size_t size, int (*compar)())
  68. {
  69.     register char c, *i, *j, *lo, *hi;
  70.     char *min, *max;
  71.  
  72.     if (n <= 1)
  73.         return;
  74.  
  75.     ix_lock_base ();
  76.     if (! semaphore_initialized++) InitSemaphore (&ss);
  77.     ix_unlock_base ();
  78.     
  79.     semaphore_initialized = 1;
  80.     ObtainSemaphore (&ss);
  81.  
  82.     qsz = size;
  83.     qcmp = compar;
  84.     thresh = qsz * THRESH;
  85.     mthresh = qsz * MTHRESH;
  86.     max = base + n * qsz;
  87.     if (n >= THRESH) {
  88.         qst(base, max);
  89.         hi = base + thresh;
  90.     } else {
  91.         hi = max;
  92.     }
  93.     /*
  94.      * First put smallest element, which must be in the first THRESH, in
  95.      * the first position as a sentinel.  This is done just by searching
  96.      * the first THRESH elements (or the first n if n < THRESH), finding
  97.      * the min, and swapping it into the first position.
  98.      */
  99.     for (j = lo = base; (lo += qsz) < hi; )
  100.         if (qcmp(j, lo) > 0)
  101.             j = lo;
  102.     if (j != base) {
  103.         /* swap j into place */
  104.         for (i = base, hi = base + qsz; i < hi; ) {
  105.             c = *j;
  106.             *j++ = *i;
  107.             *i++ = c;
  108.         }
  109.     }
  110.     /*
  111.      * With our sentinel in place, we now run the following hyper-fast
  112.      * insertion sort.  For each remaining element, min, from [1] to [n-1],
  113.      * set hi to the index of the element AFTER which this one goes.
  114.      * Then, do the standard insertion sort shift on a character at a time
  115.      * basis for each element in the frob.
  116.      */
  117.     for (min = base; (hi = min += qsz) < max; ) {
  118.         while (qcmp(hi -= qsz, min) > 0)
  119.             /* void */;
  120.         if ((hi += qsz) != min) {
  121.             for (lo = min + qsz; --lo >= min; ) {
  122.                 c = *lo;
  123.                 for (i = j = lo; (j -= qsz) >= hi; i = j)
  124.                     *i = *j;
  125.                 *i = c;
  126.             }
  127.         }
  128.     }
  129.  
  130.     ReleaseSemaphore (&ss);
  131. }
  132.  
  133. /*
  134.  * qst:
  135.  * Do a quicksort
  136.  * First, find the median element, and put that one in the first place as the
  137.  * discriminator.  (This "median" is just the median of the first, last and
  138.  * middle elements).  (Using this median instead of the first element is a big
  139.  * win).  Then, the usual partitioning/swapping, followed by moving the
  140.  * discriminator into the right place.  Then, figure out the sizes of the two
  141.  * partions, do the smaller one recursively and the larger one via a repeat of
  142.  * this code.  Stopping when there are less than THRESH elements in a partition
  143.  * and cleaning up with an insertion sort (in our caller) is a huge win.
  144.  * All data swaps are done in-line, which is space-losing but time-saving.
  145.  * (And there are only three places where this is done).
  146.  */
  147.  
  148. static int
  149. qst(char *base, char *max)
  150. {
  151.     register char c, *i, *j, *jj;
  152.     register int ii;
  153.     char *mid, *tmp;
  154.     int lo, hi;
  155.  
  156.     /*
  157.      * At the top here, lo is the number of characters of elements in the
  158.      * current partition.  (Which should be max - base).
  159.      * Find the median of the first, last, and middle element and make
  160.      * that the middle element.  Set j to largest of first and middle.
  161.      * If max is larger than that guy, then it's that guy, else compare
  162.      * max with loser of first and take larger.  Things are set up to
  163.      * prefer the middle, then the first in case of ties.
  164.      */
  165.     lo = max - base;        /* number of elements as chars */
  166.     do    {
  167.         mid = i = base + qsz * ((lo / qsz) >> 1);
  168.         if (lo >= mthresh) {
  169.             j = (qcmp((jj = base), i) > 0 ? jj : i);
  170.             if (qcmp(j, (tmp = max - qsz)) > 0) {
  171.                 /* switch to first loser */
  172.                 j = (j == jj ? i : jj);
  173.                 if (qcmp(j, tmp) < 0)
  174.                     j = tmp;
  175.             }
  176.             if (j != i) {
  177.                 ii = qsz;
  178.                 do    {
  179.                     c = *i;
  180.                     *i++ = *j;
  181.                     *j++ = c;
  182.                 } while (--ii);
  183.             }
  184.         }
  185.         /*
  186.          * Semi-standard quicksort partitioning/swapping
  187.          */
  188.         for (i = base, j = max - qsz; ; ) {
  189.             while (i < mid && qcmp(i, mid) <= 0)
  190.                 i += qsz;
  191.             while (j > mid) {
  192.                 if (qcmp(mid, j) <= 0) {
  193.                     j -= qsz;
  194.                     continue;
  195.                 }
  196.                 tmp = i + qsz;    /* value of i after swap */
  197.                 if (i == mid) {
  198.                     /* j <-> mid, new mid is j */
  199.                     mid = jj = j;
  200.                 } else {
  201.                     /* i <-> j */
  202.                     jj = j;
  203.                     j -= qsz;
  204.                 }
  205.                 goto swap;
  206.             }
  207.             if (i == mid) {
  208.                 break;
  209.             } else {
  210.                 /* i <-> mid, new mid is i */
  211.                 jj = mid;
  212.                 tmp = mid = i;    /* value of i after swap */
  213.                 j -= qsz;
  214.             }
  215.         swap:
  216.             ii = qsz;
  217.             do    {
  218.                 c = *i;
  219.                 *i++ = *jj;
  220.                 *jj++ = c;
  221.             } while (--ii);
  222.             i = tmp;
  223.         }
  224.         /*
  225.          * Look at sizes of the two partitions, do the smaller
  226.          * one first by recursion, then do the larger one by
  227.          * making sure lo is its size, base and max are update
  228.          * correctly, and branching back.  But only repeat
  229.          * (recursively or by branching) if the partition is
  230.          * of at least size THRESH.
  231.          */
  232.         i = (j = mid) + qsz;
  233.         if ((lo = j - base) <= (hi = max - i)) {
  234.             if (lo >= thresh)
  235.                 qst(base, j);
  236.             base = i;
  237.             lo = hi;
  238.         } else {
  239.             if (hi >= thresh)
  240.                 qst(i, max);
  241.             max = j;
  242.         }
  243.     } while (lo >= thresh);
  244. }
  245.